/**
 * Copyright &copy; 2016-2017 <a href="http://git.oschina.net/whoamien/backend_management">Backend Management</a> All rights reserved.
 */
package com.xw.app.service;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;
import org.springframework.stereotype.Service;

import com.xw.framework.base.dao.BaseEntity;
import com.xw.util.ShiroSecurityUtil;

/**
 * The Class UpdateFieldInterceptor.
 *
 * @author Xi Wei
 */
@Service
public class UpdateFieldInterceptor extends EmptyInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7139019699704132734L;
	private Set inserts = new HashSet();
	private Set updates = new HashSet();
	private Set deletes = new HashSet();
	
	/* (non-Javadoc)
	 * @see org.hibernate.EmptyInterceptor#onPrepareStatement(java.lang.String)
	 */
	@Override
	public String onPrepareStatement(String sql) {
		// System.out.println("execute sql: " + sql);
		return super.onPrepareStatement(sql);
	}
	
	
	
	/* (non-Javadoc)
	 * @see org.hibernate.EmptyInterceptor#onSave(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
	 */
	@Override
	public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames,
			Type[] types) {
		
/*		if(entity instanceof BaseEntity){
			for (int i = 0; i < propertyNames.length; i++) {
				if ("createUser".equals(propertyNames[i]) || "createUserId".equals(propertyNames[i])) {
					state[i] = getCurrentUser();
				} else if ("createDate".equals(propertyNames[i])) {
					if(state[i] == null)
					{
						state[i] = new Timestamp(System.currentTimeMillis());
					}
				} else if ("updateUserId".equals(propertyNames[i]) || "updateUser".equals(propertyNames[i])) {
					state[i] = getCurrentUser();
				} else if ("updateDate".equals(propertyNames[i])) {
					state[i] = new Timestamp(System.currentTimeMillis());
				} else if ("systemId".equals(propertyNames[i])) {
					if(ShiroSecurityUtil.getUserSessionInfo() != null){
						state[i] = ShiroSecurityUtil.getUserSessionInfo().getSystemId();
					}
					
				}
			}

		}
*/
		return false;	
	}
	
	
	/* (non-Javadoc)
	 * @see org.hibernate.EmptyInterceptor#onFlushDirty(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
	 */
	@Override
	public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
			String[] propertyNames, Type[] types) {

/*			if (entity instanceof BaseEntity) {

			for (int i = 0; i < propertyNames.length; i++) {

				if ("createUserId".equals(propertyNames[i]) || "createUser".equals(propertyNames[i])) {
					if (currentState[i] == null) {
						currentState[i] = getCurrentUser();
					}

				} else if ("createDate".equals(propertyNames[i])) {
					if (currentState[i] == null) {
						currentState[i] = new Timestamp(System.currentTimeMillis());
					}
				} else if ("updateUserId".equals(propertyNames[i]) || "updateUser".equals(propertyNames[i])) {
					currentState[i] = getCurrentUser();
				} else if ("updateDate".equals(propertyNames[i])) {
					currentState[i] = new Timestamp(System.currentTimeMillis());
				}else if ("systemId".equals(propertyNames[i])) {
					if(ShiroSecurityUtil.getUserSessionInfo() != null){
						currentState[i] = ShiroSecurityUtil.getUserSessionInfo().getSystemId();
					}
					
				}

			}
		}
*/
		return false;	
	}

	
	private Long getCurrentUser() {
		return ShiroSecurityUtil.getLoginId();
	}
}
